package 寒假刷题;

public class 球会落何处 {
    int m,n;
    public int[] findBall(int[][] grid) {
        m = grid.length;
        n = grid[0].length;
        int[] ans = new int[n];
        for (int j = 0; j < n; j++) {
            ans[j] = dfs(grid,0,j);
        }
        return ans;
    }
    //从第i列落下的结果
    private int dfs(int[][] grid, int i,int j) {
        //在最左边滑出去
        if (grid[i][j]==-1 && j==0)
            return -1;
        //在最右边滑出去
        if (grid[i][j]==1 && j==n-1)
            return -1;
        //形成v字形
        if (grid[i][j]!= grid[i][j+grid[i][j]])
            return -1;
        //到达最后一行
        if (i==m-1)
            return j+grid[i][j];
        return dfs(grid,i+1,j+grid[i][j]);
    }
}
